//
//  JHAudioViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHAudioViewController.h"
#import "JHSpecialViewController.h"
#import "JHHotUserViewController.h"
#import "JHHotMusicViewController.h"
#import "JHAblumViewController.h"
#import "JHRecommendTableViewCell.h"
#import "JHREcommend.h"
#import "NetwordHandler.h"
//  轮播图
#import "JHLunbotuCollectionViewCell.h"
#import "JHlunboTU.h"
//  专题
#import "JHZhuanti.h"
#import "JHZhuantiTableViewCell.h"

@interface JHAudioViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UIScrollView *audioScroll;
@property(nonatomic,retain)UICollectionView *specialColltionView;
@property(nonatomic,retain)UIButton *hotUser;
@property(nonatomic,retain)UIButton *hotMusic;
@property(nonatomic,retain)UITableView *spcialTableView;
@property(nonatomic,retain)UITableView *audioTableView;
//  大字典
@property(nonatomic,retain)NSMutableDictionary *Dic;
//  轮播图数组
@property(nonatomic,retain)NSMutableArray *lunbotuArr;
//  推荐专辑数组
@property(nonatomic,retain)NSMutableArray *recommendArr;
//  专题
@property(nonatomic,retain)JHZhuanti *zt;
// 定时器
@property (nonatomic, retain) NSTimer *time;
@end

@implementation JHAudioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    self.title = @"音乐";

    [self creatAblum];
    
    [self dian];


}
- (void)creatAblum
{
    self.audioScroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];

    _audioScroll.bounces = NO;
       NSLog(@"%lf",_audioTableView.contentSize.height);
    [self.view addSubview:_audioScroll];
    [self getDataFromRows];
    
}

-(void)getDataFromRows
{
   
    //获取数据
    [NetwordHandler requestWithURL:@"http://mobile.ximalaya.com/m/super_explore_index2?picVersion=5&scale=2" completion:^(id result) {
//        NSLog(@"%@",result);
        /**
         *  创建大字典
         */
        self.Dic = [NSMutableDictionary dictionary];
        _Dic = result;
        //  获取大字典中轮播图数组
        NSMutableArray *arr1 = [[_Dic objectForKey:@"focusImages"] objectForKey:@"list"];
        self.lunbotuArr = [NSMutableArray array];
        for (NSDictionary *lun in arr1) {
            JHlunboTU *botu = [[JHlunboTU alloc] initWithDic:lun];
            [_lunbotuArr addObject:botu];
        }
        [self createScrollView];
        //  去出大字典中的推荐专题
        NSMutableArray *arr2 = [[_Dic objectForKey:@"recommendAlbums"] objectForKey:@"list"];
        self.recommendArr = [NSMutableArray array];
        for (NSDictionary *d in arr2) {
            JHREcommend *rd = [[JHREcommend alloc] initWithDic:d];
            [_recommendArr addObject:rd];
        }
            [self createAudio];
        
        //  最火主播和歌曲
        [self createImage];
        //  获取字典中的专题
        
        self.zt = [[JHZhuanti alloc] initWithDic:[_Dic objectForKey:@"latest_special"]];
        NSLog(@"%@",_zt.coverPathSmall);
//        [self createSpcial];
//        NSLog(@"----------%lf",_audioTableView.contentSize.height);
        _audioScroll.contentSize = CGSizeMake(0, _audioTableView.contentSize.height+ _spcialTableView.contentSize.height + _specialColltionView.frame.size.height + _hotUser.frame.size.height + 160);
        
        [self.indicatorView removeFromSuperview];

    }];
}
- (void)createScrollView
{
    //  创建轮播图coll
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.frame.size.width - 20, self.view.frame.size.height / 3);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    
    self.specialColltionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height / 3) collectionViewLayout:layout] autorelease];
    _specialColltionView.pagingEnabled = YES;
    _specialColltionView.backgroundColor = [UIColor whiteColor];
    _specialColltionView.userInteractionEnabled = NO;
    _specialColltionView.delegate = self;
    _specialColltionView.dataSource = self;
    [_audioScroll addSubview:_specialColltionView];
    [_specialColltionView registerClass:[JHLunbotuCollectionViewCell class] forCellWithReuseIdentifier:@"special"];
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(runPhotoes:) userInfo:nil repeats:YES];

}
- (void)runPhotoes:(NSTimer *)time
{
//    NSInteger i = 0;
    
    _specialColltionView.contentOffset = CGPointMake(_specialColltionView.contentOffset.x +(self.view.frame.size.width - 20), 0);
    
    if (_specialColltionView.contentOffset.x == (self.view.frame.size.width - 20)*6) {
        _specialColltionView.contentOffset = CGPointMake(self.view.frame.size.width - 20, 0);
//        i = 0;
    }
//    i++;
//    NSInteger i = 0;
//    i++;
//    [_specialColltionView setContentOffset:CGPointMake((self.view.frame.size.width - 20) * i, 0) animated:YES];
}
//  轮播图
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //  轮播图行数
    return _lunbotuArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //  轮播图赋值
    JHLunbotuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"special" forIndexPath:indexPath];
    cell.lunbotu = _lunbotuArr[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //  轮播图触发方法
    
}

//  创建轮播图下面的两个图片
- (void)createImage
{
    //  创建最火主播
    self.hotUser = [UIButton buttonWithType:UIButtonTypeCustom];
    _hotUser.frame = CGRectMake(10, _specialColltionView.frame.size.height + 20, (self.view.frame.size.width / 2) -20 , self.view.frame.size.height / 7);
//    _hotUser.backgroundColor = [UIColor yellowColor];
    [_hotUser setImage:[UIImage imageNamed:@"BDBBB757-8F5F-4F09-A039-5FC888904A04"] forState:UIControlStateNormal];
    [_hotUser addTarget:self action:@selector(User) forControlEvents:UIControlEventTouchUpInside];
    [_audioScroll addSubview:_hotUser];
    //  创建最热歌曲
    self.hotMusic = [UIButton buttonWithType:UIButtonTypeCustom];
    _hotMusic.frame = CGRectMake(_hotUser.frame.size.width + 30, _specialColltionView.frame.size.height + 20, (self.view.frame.size.width / 2) -20 , self.view.frame.size.height / 7);
//    _hotMusic.backgroundColor = [UIColor blueColor];
    [_hotMusic setImage:[UIImage imageNamed:@"03A9CE93-420F-40B8-8C02-85C952C34465"] forState:UIControlStateNormal];
    [_hotMusic addTarget:self action:@selector(Music) forControlEvents:UIControlEventTouchUpInside];
    [_audioScroll addSubview:_hotMusic];
    
}
//  最火主播的触发方法
- (void)User
{
    JHHotUserViewController *hotUser = [[JHHotUserViewController alloc] init];
    [self.navigationController pushViewController:hotUser animated:YES];
}
//  最热歌曲的触发方法
- (void)Music
{
    JHHotMusicViewController *music = [[JHHotMusicViewController alloc] init];
    [self.navigationController pushViewController:music animated:YES];
}
- (void)createSpcial
{
    //  创建专题TableView
    self.spcialTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, _specialColltionView.frame.size.height + _hotUser.frame.size.height + 40, self.view.frame.size.width - 20, 90) style:UITableViewStylePlain];
    _spcialTableView.delegate = self;
    _spcialTableView.dataSource = self;
    _spcialTableView.scrollEnabled = NO;
    [_spcialTableView registerClass:[JHZhuantiTableViewCell class] forCellReuseIdentifier:@"spcial"];
    [_audioScroll addSubview:_spcialTableView];
}

- (void)createAudio
{

    //  创建推荐专辑TableView
    self.audioTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 3 + self.view.frame.size.height / 7  + 50, self.view.frame.size.width, _recommendArr.count * 62) style:UITableViewStyleGrouped];
    _audioTableView.delegate = self;
    _audioTableView.dataSource = self;
    _audioTableView.scrollEnabled = NO;
    [_audioTableView registerClass:[JHRecommendTableViewCell class] forCellReuseIdentifier:@"audio"];
    [_audioScroll addSubview:_audioTableView];
    
    UILabel *view1 = [[[UILabel alloc] initWithFrame:CGRectMake(0,_audioTableView.frame.origin.y - 30, self.view.frame.size.width, 30)] autorelease];
    view1.text = @"    推荐专辑";
    view1.backgroundColor = [UIColor whiteColor];
    [_audioScroll addSubview:view1];
}
//  推荐专辑
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //  推荐专辑和专题行数
    if ([tableView isEqual:_spcialTableView]) {
        return 1;
    }else
        if ([tableView isEqual:_audioTableView]) {
            return _recommendArr.count;
        }return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _audioTableView) {
        return 60;
    }else
        if (tableView == _spcialTableView) {
            return 100;
        }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  推荐专辑赋值
    if (tableView == _audioTableView) {

        JHRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"audio"];
        cell.recom = _recommendArr[indexPath.row];
        return cell;
    }else
        //  专题赋值
        if (tableView == _spcialTableView) {
         JHZhuantiTableViewCell *c = [tableView dequeueReusableCellWithIdentifier:@"spcial"];
            c.zhuanti = _zt;
//            NSLog(@"3333333%@",c.zhuanti.coverPathSmall);
            return c;
        }return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     //  推荐专辑方法
    if ([tableView isEqual:_audioTableView]) {
        JHAblumViewController *ablum = [[JHAblumViewController alloc] init];
        ablum.albumId = [_recommendArr[indexPath.row] recommendId];
        NSLog(@"%@",[_recommendArr[indexPath.row] recommendId]);
        [self.navigationController pushViewController:ablum animated:YES];
    }else
        //  专题方法
        if ([tableView isEqual:_spcialTableView]) {
            JHSpecialViewController *special = [[JHSpecialViewController alloc] init];
            [self.navigationController pushViewController:special animated:YES];
        }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
